<?php
class ReaderController extends Controller{
	//分页展示所有读者
	public function indexAction(){
		$title="读者管理";
		$include_file="allreader";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$readermodel=new ReaderModel("tb_reader");
		
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $readermodel->total("");
		$allreaders = $readermodel->getAllReaders($offset, $pagesize);
		//载入工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"reader","a"=>"index"));
		$pageinfo=$page->showPage();
		include VIEWS_PATH."index.html";
	}

	//载入添加读者表单
	public function addAction(){
		$title="添加读者";
		$include_file="addreader";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//获取全部读者类型
		$readertypeModel=new ReaderTypeModel("tb_readertype");
		$types=$readertypeModel->getAllReaderType();
		
		
		include VIEWS_PATH."index.html";
	}

	//插入数据
	public function insertAction(){
		$data['barcode']=$_POST['barcode']+0;
		$data['name']=$_POST['name'];
		$data['typeid']=$_POST['typeid']+0;
		$data['sex']=$_POST['sex'];
		$data['vocation']=$_POST['vocation'];
		$data['birthday']=$_POST['birthday'];
		$data['paperType']=$_POST['paperType'];
		$data['paperNO']=$_POST['paperNO'];
		$data['tel']=$_POST['tel']+0;
		$data['email']=$_POST['email'];
		$data['createDate']=date("Y-m-d");		
		$data['remark']=$_POST['remark'];
		$data['operator']=$_SESSION['admin_name'];

		//判断是否为空
		if (empty($data['barcode'])||strlen($data['barcode']) > 9) {
			$this->jump("index.php?c=reader&a=add","读者条形码不不合法!",3);
		}else if (empty($data['name'])){
			$this->jump("index.php?c=reader&a=add","读者姓名不能为空!",3);
		}
		else if (empty($data['paperType'])||empty($data['paperNO'])){
			$this->jump("index.php?c=reader&a=add","读者证件类型/证件号码不能为空!",3);
		}
		else if (empty($data['typeid'])){
			$this->jump("index.php?c=reader&a=add","读者类型不能为空!",3);
		}
		else if (empty($data['tel'])&&empty($data['email'])){
			$this->jump("index.php?c=reader&a=add","请至少填写一项读者的联系方式!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$readermodel=new ReaderModel("tb_reader");
		if ($readermodel->insert($data)) {
			$this->jump("index.php?c=reader&a=index","添加成功!",3);
		}else {
			$this->jump("index.php?c=reader&a=index","添加失败,请刷新后再试!",3);
		}
	}

	//修读者表单
	public function editAction(){
		$title="修改读者信息";
		$include_file="editreader";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//获取全部读者类型
		$readertypeModel=new ReaderTypeModel("tb_readertype");
		$types=$readertypeModel->getAllReaderType();
		//获取根据主键ID获取欲记录
		$pk=$_GET['id']+0;
		$readermodel=new ReaderModel("tb_reader");
		$data=$readermodel->selectByPk($pk);
		
		include VIEWS_PATH."index.html";
	}

	//完成读者信息更新
	public function updateAction(){
		$data['barcode']=$_POST['barcode']+0;
		$data['name']=$_POST['name'];
		$data['typeid']=$_POST['typeid']+0;
		$data['sex']=$_POST['sex'];
		$data['vocation']=$_POST['vocation'];
		$data['birthday']=$_POST['birthday'];
		$data['paperType']=$_POST['paperType'];
		$data['paperNO']=$_POST['paperNO'];
		$data['tel']=$_POST['tel']+0;
		$data['email']=$_POST['email'];
		$data['createDate']=date("Y-m-d");		
		$data['remark']=$_POST['remark'];
		$data['operator']=$_SESSION['admin_name'];
		$data['id']=$_POST['id']+0;

		//判断是否为空
	if (empty($data['barcode'])) {
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","读者条形码不能为空!",3);
		}else if (empty($data['name'])){
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","读者姓名不能为空!",3);
		}
		else if (empty($data['paperType'])||empty($data['paperNO'])){
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","读者证件类型/证件号码不能为空!",3);
		}
		else if (empty($data['typeid'])){
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","读者类型不能为空!",3);
		}
		else if (empty($data['tel'])&&empty($data['email'])){
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","请至少填写一项读者的联系方式!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$readermodel=new ReaderModel("tb_reader");
		if ($readermodel->update($data)) {
			$this->jump("index.php?c=reader&a=index","修改成功!",2);
		}else {
			$this->jump("index.php?c=reader&a=edit&id={$data['id']}","修改失败!",3);
		}
	}

	//删除读者
	public function deleteAction(){
		$pk=$_GET['id']+0;
		$readermodel=new ReaderModel("tb_reader");
		if ($readermodel->delete($pk)) {
			$this->jump("index.php?c=reader&a=index", "删除成功!",2);
		}else {
			$this->jump("index.php?c=reader&a=index", "删除失败,请刷新页面后重试!",3);
		}
	}
}